package edu.zust.zw.shop.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;

import org.apache.tomcat.jni.OS;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;

import edu.zust.zw.shop.db.dao.domain.ShopUserDomain;
import edu.zust.zw.shop.service.PicService;

@Service
public class PicServiceImpl implements PicService {
    
    private static final List<String> PIC_SUFFIX_LIST = Lists.newArrayList("jpg", "bmp", "png", "jpeg", "gif");

    @Override
    public String uploadPic(String uploadUrl, File file, ShopUserDomain user) throws FileNotFoundException, IOException {
        // TODO Auto-generated method stub
        String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
        if (PIC_SUFFIX_LIST.contains(suffix)) {
            FileImageInputStream fis = new FileImageInputStream(file);
            String picUrl = uploadUrl + "/" + user.getUserId() + "_" + System.currentTimeMillis() + "." + suffix;
            File newFile = new File(picUrl);
            if (!newFile.exists()) {
                newFile.createNewFile();
            }
            int temp = 0;
            byte[] tempByte = new byte[1024];
            FileImageOutputStream fos = new FileImageOutputStream(newFile);
          //  ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while ((temp = fis.read(tempByte)) != -1) {
                fos.write(tempByte, 0, temp);
            }
    
          //  fos.write(bos.toByteArray(), 0, bos.toByteArray().length);
            fos.close();
            fis.close();
            return picUrl;
        }
        
        return null;
    }

    @Override
    public String uploadPic(String uploadUrl, MultipartFile file, ShopUserDomain user)
            throws FileNotFoundException, IOException {
        // TODO Auto-generated method stub
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        System.out.println(suffix);
        if (PIC_SUFFIX_LIST.contains(suffix)) {
            InputStream is = file.getInputStream();
            Long currentTime = System.currentTimeMillis();
            String picUrl = uploadUrl + "/" + user.getUserId() + "_" + currentTime + "." + suffix;
            String returnUrl = "http://39.108.133.76/pic/" + user.getUserId() + "_" + currentTime + "." + suffix;
            byte[] tempByte = new byte[1024];
            int temp = 0;
            File newFile = new File(picUrl);
            if (!newFile.exists()) {
                newFile.createNewFile();
            }
            FileImageOutputStream fos = new FileImageOutputStream(newFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while ((temp = is.read(tempByte)) != -1) {
                bos.write(tempByte, 0, temp);
            }
            fos.write(bos.toByteArray(), 0, bos.toByteArray().length);
            fos.close();
            bos.close();
            is.close();
            
            return  returnUrl;
        } else {
            return null;
        }
    }

}
